package com.yunxi.service.cms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.cms.*;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.cms.CmsService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2019/9/1.
 */
@Service
@Qualifier("cmsServiceImpl")
public class CmsServiceImpl implements CmsService {
    private static final Logger logger = LoggerFactory.getLogger(CmsServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;

    @Override
    public ApiResult distributeCms(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.CMS_PMS_PROJECT)) {
            //CMS传递项目信息给PMS
            apiResult = saveProject(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_CATALOGCASE)){
         //工时目录信息
            apiResult = saveManHourCatalogcase(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_DICTIONARY)){
            //工时条目信息
            apiResult = saveManHourDictionary(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_STEP)){
            //工时步骤信息
            apiResult = saveManHourStep(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_BASE_MAN_HOUR_STEP)){
            //工时基础步骤信息
            apiResult = saveBaseManHourStep(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_EPC_DICTIONARY)){
            //EPC目录
            apiResult = saveEpcDictionary(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_EPC_USAGE)){
            //EPC零件用法
            apiResult = saveEpcUsage(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_EPC_USAGE)){
            //EPC零件用法
            apiResult = saveEpcUsage(esbVO);
        }else if (serviceName.equals(Constant.CMS_PMS_EPC_REPLACE)){
            //EPC替换关系
            apiResult = saveEpcReplace(esbVO);
        }else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult saveEpcReplace(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<EpcReplaceVO> epcReplaceList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcReplaceVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC替换关系给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveEpcUsage(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<EpcDictionaryVO> epcDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcDictionaryVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveEpcDictionary(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<EpcDictionaryVO> epcDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcDictionaryVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveBaseManHourStep(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<BaseManHourStepVO> baseManHourStepList = JSON.parseObject(reqBody, new TypeReference<ArrayList<BaseManHourStepVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时基础步骤信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveManHourStep(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<ManHourStepVO> manHourStepList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourStepVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时步骤信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveManHourDictionary(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<ManHourDictionaryVO> manHourDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourDictionaryVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时条目信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveManHourCatalogcase(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
       // ManHourCatalogcaseVO manHourCatalogcaseVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), ManHourCatalogcaseVO.class);
        ArrayList<ManHourCatalogcaseVO> manHourCatalogcaseList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourCatalogcaseVO>>() {});
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveProject(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ProjectVO projectVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), ProjectVO.class);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递项目信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            apiResult =ResponseUtil.ok();
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }

}
